package cn.unknowpupil.init.controller.monitor;

import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.enums.CodeEnum;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.domain.dto.EmailDto;
import cn.unknowpupil.init.domain.system.SysEmail;
import cn.unknowpupil.init.domain.vo.EmailVo;
import cn.unknowpupil.init.service.system.ISysEmailService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created at 2020-11-04 by  w10g <br>
 * Initialize SysEmail  ... <br>
 *  邮件配置表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-邮件配置表模块" }, value = "SysEmail")
@RestController
@RequestMapping(value = "admin/sys-email")
public class SysEmailController {


    @Resource
    private ISysEmailService sysEmailService;

    /**
     * 新增
     *
     * @param sysEmail {@link SysEmail}  邮件配置表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "新增邮件配置表", businessType = BusinessType.INSERT)
    @PreAuthorize("@ps.hasPerm('baseinfo:email:create')")
    @PostMapping("create")
    public ResultResponse create(@Validated @RequestBody SysEmail sysEmail) {


        // 业务逻辑
        boolean created = sysEmailService.create(sysEmail);
        if (created) {
             return ResultResponse.success("创建成功");
        }else{
             return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param sysEmail {@link SysEmail}  邮件配置表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @PreAuthorize("@ps.hasPerm('baseinfo:email:update')")
    @Log(title = "修改邮件配置表", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysEmail sysEmail) {

        // 业务逻辑
        boolean update = sysEmailService.update(sysEmail);
        if (update) {
             return ResultResponse.success("修改成功");
        }else{
             return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 修改
     *
     * @param sysEmail {@link SysEmail}  邮件配置表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改开启状态")
    @PreAuthorize("@ps.hasPerm('baseinfo:email:update')")
    @Log(title = "修改邮件开启状态", businessType = BusinessType.UPDATE)
    @PutMapping("change/status")
    public ResultResponse changeStatus(@Validated @RequestBody SysEmail sysEmail) {

        // 业务逻辑
        boolean update = sysEmailService.changeStatus(sysEmail);
        if (update) {
            return ResultResponse.success("修改成功");
        }else{
            return ResultResponse.buildFailure("修改失败");
        }

    }
    /**
     * 获取
     *
     * @param id {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{id}")
    public ResultResponse get(@PathVariable("id") Long id) {
        SysEmail sysEmail = sysEmailService.info(id);
        return ResultResponse.buildSuccess(sysEmail);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @PreAuthorize("@ps.hasPerm('baseinfo:email:delete')")
    @Log(title = "删除邮件配置", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted  = sysEmailService.delete(ids);
        if(deleted){
            return ResultResponse.success("删除成功");
        }else{
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
        * 分页
        *
        * @param current {@code int} 页码
        * @param size    {@code int} 笔数
        * @return {@link ResultResponse}
        */
    @ApiOperation("系统：分页")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysEmail>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysEmail sysEmail) {
            IPage<SysEmail> page = sysEmailService.page(current, size, sysEmail);
            return ResultResponse.buildSuccess(page);
    }


    @PostMapping(value = "reset/email")
    @ApiOperation("重置邮箱，发送验证码")
    @PreAuthorize("@ps.hasPerm('baseinfo:email:update')")
    @Log(title = "重置邮箱，发送验证码", businessType = BusinessType.INSERT)
    public ResultResponse resetEmail(@RequestBody String email){
        EmailVo emailVo = sysEmailService.sendEmail(email, CodeEnum.EMAIL_RESET_EMAIL_CODE.getKey());
        sysEmailService.send(emailVo,sysEmailService.find());
        return ResultResponse.success("发送验证码成功");
    }


    @PostMapping(value = "send/email")
    @ApiOperation("测试，邮件发送")
    @Log(title = "测试，邮件发送", businessType = BusinessType.INSERT)
    public ResultResponse sendEmail(@Validated @RequestBody EmailDto emailDto){
        EmailVo emailVo = sysEmailService.sendEmail(emailDto);
        sysEmailService.send(emailVo,sysEmailService.getById(emailDto.getId()));
        return ResultResponse.success("发送验证码成功");
    }

}
